package org.kjtc.mapper;

import org.apache.ibatis.annotations.*;
import org.kjtc.entity.*;

import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/21 0021.
 */
@Mapper
public interface BackLogMapper {

      @Select("select productName,productId from product")
      List<Product> getAllProducts();

      @Select("select productVersionID,productVersionName from productversion where ProductID=#{ProductID} ORDER BY CreateDTTM")
      List<ProductVersion> getAllProductVersions(String productID);

      @Select("select userName,userID from userinfo")
      List<UserInfo> getAllMembers();

      @SelectProvider(type =AllProvider.class, method = "getBackLog")
      List<BackLog> getBackLog(@Param("productVersionId") String productVersionId
              , @Param("MemberId") String MemberId, @Param("BacklogStatus") String BacklogStatus);

     @SelectProvider(type =AllProvider.class, method = "getBackLogFeed")
     List<Map<String,Object>> getBackLogFeed(String backLogId);


     @Update("update backlog set ActualStartDTTM=ifnull(ActualStartDTTM,NOW()),ActualWorkDate=ifnull(ActualWorkDate,0)+1,UpdateDTTM=now() " +
             "where BacklogID=#{backLogId}")
     int updateActualTime(String backLogId);

    @Select("SELECT ActualStartDTTM FROM backlog WHERE BacklogID=#{backLogId}")
     List<BackLog> selectBackLogDTTM(String backLogId);

     @Select("select BackLogID from punchlog " +
             "WHERE DATE_FORMAT(PunchDTTM,'%Y-%m-%d')=DATE_FORMAT(now(),'%Y-%m-%d') and PunchUserID=#{userId}")
     List<PunchLog> getPunchEveryDay(String userId);

    @Select("select UserID from backlog WHERE BacklogID=#{backLogId}")
    List<BackLog> getUserIDByBackLogID(String backLogId);


    @Select("select u.UserName,fb.* from feedback fb LEFT JOIN module md on fb.ModuleID=md.ModuleID " +
            "LEFT JOIN userinfo u on u.UserID=fb.FeedBackUser " +
            "WHERE md.ProductVersionID=#{productVersionId} and fb.FeedBackStatus='0'")
    List<Map<String,Object>> getFeedByProductVersionId(String productVersionId);

    @SelectProvider(type =AllProvider.class, method="getBackLogProductVersionId")
    List<BackLog> getBackLogProductVersionId(@Param("productVersionId") String productVersionId);

    @SelectProvider(type =AllProvider.class, method = "getBackLogByUser")
    List<BackLog> getBackLogByUser(BackLog backLog);

    @Insert("INSERT into punchlog (punchUserID,backLogID,punchDTTM)values(#{userName},#{backLogId},now())")
    int addPunchRecord(@Param("userName") String userName, @Param("backLogId") String backLogId);

    @Update("UPDATE backlog set BacklogStatus=#{backLogState},UpdateUser=#{userID},UpdateDTTM=now() WHERE BacklogID=#{backLogID}")
     int updateBackLogState(@Param("backLogState") String backLogState, @Param("backLogID") String backLogID,@Param("userID")String userID);

    @Update("UPDATE backlog set BacklogStatus=#{backLogState},ActualEndDTTM=now(),UpdateUser=#{userID},UpdateDTTM=now() WHERE BacklogID=#{backLogID}")
    int updateBackLogEndDTTM(@Param("backLogState") String backLogState, @Param("backLogID") String backLogID,@Param("userID")String userID);

    @Update("UPDATE backlog set UserID=#{UserID},UpdateDTTM=now(),UpdateUser=#{updateUser} WHERE BacklogID=#{backLogID}")
    int updateBackLogUser(@Param("backLogID") String backLogID, @Param("UserID") String UserID,@Param("updateUser")String updateUser);

    @Select("select pd.ProductName,pv.ProductVersionName from productversion pv,product pd WHERE pv.ProductVersionID=#{productVersionId} and pv.ProductID=pd.ProductID")
    List<BackLog> getCurrentTitle(String productVersionId);








}
